///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_THREADS_CMULTIQUEUE_H__
#define __BUOLA_THREADS_CMULTIQUEUE_H__

#include <buola/buola.h>
#include <buola/threads/condition_variable.h>

namespace buola {

template<typename tKey,typename tValue>
class CMultiQueue
{
    typedef std::map<tKey,tValue> tDelivered;

    struct SWaitElem
    {
        bool mDelivered;
        tValue mValue;
    };

    typedef std::map<tKey,SWaitElem*> tWaited;
    
public:
    CMultiQueue()
        :   mCancelled(false)
    {}

    bool WaitFor(tKey pKey,tValue &pValue)
    {
        unique_lock<mutex> lLock(mMutex);
        
        typename tDelivered::iterator lFind=mDelivered.find(pKey);
        
        if(lFind!=mDelivered.end())
        {
            pValue=lFind->second;
            mDelivered.erase(lFind);
            return true;
        }
        
        SWaitElem lElem;
        lElem.mDelivered=false;

        mWaited.insert(std::make_pair(pKey,&lElem));
        
        while(!lElem.mDelivered&&!mCancelled)
        {
            mCondVar.wait(lLock);
        }

        if(lElem.mDelivered)
        {
            pValue=lElem.mValue;
            return true;
        }

        //we were cancelled
        mToCancel--;
        mCondVar.notify_all();
        return false;
    }

    void Deliver(tKey pKey,const tValue &pValue)
    {
        lock_guard<mutex> lLock(mMutex);

        if(mCancelled) return;
        
        typename tWaited::iterator lFind=mWaited.find(pKey);
        
        if(lFind!=mWaited.end())
        {
            lFind->second->mDelivered=true;
            lFind->second->mValue=pValue;
            mWaited.erase(lFind);
            mCondVar.notify_all();
            return;
        }
        
        mDelivered.insert(std::make_pair(pKey,pValue));
    }

    void CancelAll()
    {
        unique_lock<mutex> lLock(mMutex);
        mCancelled=true;
        mCondVar.notify_all();

        mToCancel=mWaited.size();
        while(mToCancel)
        {
            mCondVar.wait(lLock);
        }
    }

private:
    tDelivered mDelivered;
    tWaited mWaited;

    mutex mMutex;
    condition_variable mCondVar;
    bool mCancelled;
    int mToCancel;
};

/*namespace buola*/ }

#endif
